//
//  MYBaseSearchBar.m
//  JQLive
//
//  Created by Bruce on 2020/2/26.
//  Copyright © 2020 mlqk. All rights reserved.
//

#import "MYBaseSearchBar.h"

@interface MYBaseSearchBar ()

@end
@implementation MYBaseSearchBar

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.placeholder = @"搜索";
        self.keyboardType = UIKeyboardTypeDefault;
        self.backgroundImage = [self getImageWithColor:RGBACOLOR(0xf6f7fb) andHeight:36];
        //设置顶部搜索栏的背景色
//        [self setBackgroundColor:RGBACOLOR_SearchBack];
        //设置顶部搜索栏输入框的样式
        UITextField *searchField;
        if (@available(iOS 13.0, *)) {
           searchField =  self.searchTextField;
            searchField.backgroundColor = RGBACOLOR(0xf6f7fb);

        }
        else
        {
            searchField = [self valueForKey:@"_searchField"];
            //设置顶部搜索栏输入框的样式
            searchField.backgroundColor = RGBACOLOR(0xf6f7fb);

        }
//        searchField.layer.borderWidth = 0.5f;
//        searchField.layer.borderColor = [RGBACOLOR(0xdfdfdf) CGColor];
//        searchField.layer.cornerRadius = 22.f;
//        searchField.layer.masksToBounds = YES;
        
//        UIView* backgroundView = [self subViewOfClassName:@"_UISearchBarSearchFieldBackgroundView"];
//        backgroundView.layer.cornerRadius = 22.f;
//        backgroundView.clipsToBounds = YES;
    }
    return self;
}


- (UIImage *)getImageWithColor:(UIColor *)color andHeight:(CGFloat)height {
    CGRect r = CGRectMake(0.0f, 0.0f, 1.0f, height);
    UIGraphicsBeginImageContext(r.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, r);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

@end

